// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <pollux/dwio/dwrf/reader/column_reader.h>
#include <pollux/vector/constant_vector.h>

namespace kumo::pollux::dwrf {

class NullColumnReader : public ColumnReader {
 public:
  NullColumnReader(
      const StripeStreams& stripe,
      const std::shared_ptr<const Type>& type)
      : ColumnReader(
            stripe.getMemoryPool(),
            dwio::common::TypeWithId::create(type)) {}
  ~NullColumnReader() override = default;

  uint64_t skip(uint64_t numValues) override {
    return numValues;
  }

  void next(uint64_t numValues, VectorPtr& result, const uint64_t* nulls)
      override {
    if (result && result->encoding() == VectorEncoding::Simple::CONSTANT &&
        result->is_null_at(0)) {
      // If vector already exists and contains the right value, resize.
      result->resize(numValues);
    } else {
      auto value_vector = BaseVector::create(fileType_->type(), 1, &memoryPool_);
      value_vector->set_null(0, true);
      result = BaseVector::wrap_in_constant(numValues, 0, value_vector);
    }
  }
};

} // namespace kumo::pollux::dwrf
